package cn.crc.pjblog.components.table;

import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.services.FormSupport;

import cn.crc.model.BlogContent;
/**
 * 分页的组件的显示数据区域，目前是把数据显示的格式固化到这个组件里面
 * 还不具备一定的通用性
 * @author doublel
 * @version 0.0.1
 * */
public class TableBlogGrid extends AbstractField{
	
	@Parameter(required=true,defaultPrefix="literal")
	private int _pageSize;
	
//	@Persist("client")
//	@Parameter()
//	private int _curPageNum=1;
	
	
	private BlogContent _curBlogContent;
	/**
	 * 用于分页显示的数据源
	 * {@value}
	 * */
	@Parameter(required=true)
	private List<BlogContent> _source;
	
	/**
	 * 引入分页显示的导航页面
	 * */
	@Component(parameters={"totalRowCount=source.size()","nPageSize=pageSize","curPageNum=curIndex"})
	private TablePages _tablePages;
	
	
	private List<BlogContent> curListItems;
	
	private Iterator _iterator;
	/**
	 * 分页的当前页数
	 * */
	@Persist("client")
	private int curIndex=1;
	/**
	 * 初始化该组件的一些参数设置
	 * 取得当前页的所有记录，保存到当前的iterator中
	 * @author doublel
	 * */
	public void setupRender(){
		
		System.out.println("当前页面=="+ this.curIndex);		
		if (this._source.size()<= this._pageSize) {
			
			this.curListItems = this._source;
		}else{
			int maxSize=0;
			if (((this.curIndex)*this._pageSize)>=this._source.size())
			{
				maxSize =this._source.size();
			}else
			{
				maxSize =((this.curIndex)*this._pageSize);
				
			}
			
			int minSize =0;
			if ((this.curIndex-1)*this._pageSize<=0) 
			{
				minSize =0;
			}else
			{
				minSize = (this.curIndex-1)*this._pageSize;
			}
			this.curListItems = this._source.subList(minSize, maxSize);
		}

		this._iterator = this.curListItems.iterator();
	}
	
	/**
	 * 获得当前行显示的数据对象
	 * */
	public void beginRender(){
		
		this._curBlogContent = (BlogContent) this._iterator.next();
		//this.curIndex ++;
	}
	
	/**
	 * 如果当前的记录没有循环结束就返回到beginRender
	 * 继续循环
	 * @author doublel
	 * */
	public boolean afterRender(){
		
		if (this._iterator.hasNext()){
			
			return false;
		}
		else{
			return true;
		}
		
	}
	
	/**
	 * 组件显示结束，清空组件显示的临时变量
	 * */
	public void cleanupRender(){
		
		this._iterator = null;
		this.curListItems = null;
		this.curIndex =1;
	}

		
	

	/* (non-Javadoc)
	 * @see org.apache.tapestry.corelib.base.AbstractField#processSubmission(org.apache.tapestry.services.FormSupport, java.lang.String)
	 * 获得分页的下一页的页数
	 */
//	@Override
//	protected void processSubmission(FormSupport formSupport, String elementName) {
//		// TODO Auto-generated method stub
////		System.out.println("this._tablePages.getCurPageNum()="+this._tablePages.getCurPageNum());
//		this.curIndex = this._tablePages.getCurPageNum();
////		this._curPageNum = this._tablePages.getCurPageNum()+1;
////		System.out.println("this.__curPageNum="+this._curPageNum);
//	}




	/* (non-Javadoc)
	 * @see org.apache.tapestry.corelib.base.AbstractField#processSubmission(java.lang.String)
	 */
	@Override
	protected void processSubmission(String arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return _pageSize;
	}


	/**
	 * @param curPageNum the curPageNum to set
	 */
//	public void setCurPageNum(int curPageNum) {
//		_curPageNum = curPageNum;
//	}
//




	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		_pageSize = pageSize;
	}


	/**
	 * @return the source
	 */
	public List<BlogContent> getSource() {
		return _source;
	}


	/**
	 * @param source the source to set
	 */
	public void setSource(List<BlogContent> source) {
		_source = source;
	}


	/**
	 * @return the curBlogContent
	 */
	public BlogContent getCurBlogContent() {
		return _curBlogContent;
	}


	/**
	 * @param curBlogContent the curBlogContent to set
	 */
	public void setCurBlogContent(BlogContent curBlogContent) {
		_curBlogContent = curBlogContent;
	}


	/**
	 * @return the curIndex
	 */
	public int getCurIndex() {
		return curIndex;
	}


	/**
	 * @param curIndex the curIndex to set
	 */
	public void setCurIndex(int curIndex) {
		this.curIndex = curIndex;
	}
	
	
	
	
	
}
